package com.gec.oasys.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.gec.oasys.dao.SysMenuMapper;
import com.gec.oasys.dao.SysMenuMapper;
import com.gec.oasys.pojo.SysDepartMent;
import com.gec.oasys.pojo.SysMenu;
import com.gec.oasys.pojo.SysMenu;
import com.gec.oasys.pojo.SysRoleMenuKey;
import com.gec.oasys.service.SysMenuService;
import com.gec.oasys.util.SoulPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class SysMenuServiceImpl implements SysMenuService {
    @Autowired
    private SysMenuMapper sysMenuMapper;
    @Override
    public SysMenu selectByPrimaryKey(Integer id) {
        return sysMenuMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<SysMenu> querySysMenu(SysMenu sysMenu) {
        return sysMenuMapper.querySysMenu(sysMenu);
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return sysMenuMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int deletesByPrimaryKey(String[] strId) {
        for (String strid : strId) {
            sysMenuMapper.deleteByPrimaryKey(Integer.parseInt(strid));
        }
        return strId.length;
    }

    @Override
    public int insert(SysMenu record){
        return sysMenuMapper.insert(record);
    }
    @Override
    public int updateByPrimaryKey(SysMenu record){
        return sysMenuMapper.updateByPrimaryKey(record);
    }
    @Override
    public Object dataGrid(SoulPage<SysMenu> soulPage) {
        return soulPage.setData(sysMenuMapper.dataGrid(soulPage,(SysMenu) soulPage.getObj()));
    }
    
    @Override
    public List<SysMenu> findMenuPermssionByRoleId(Integer roleId) {
        return sysMenuMapper.findMenuPermssionByRoleId(roleId);
    }

    @Override
    public List<SysMenu> getSysMenuTreeDateById(Integer id,Integer roleId){
        List<SysMenu> childSysMenuList=sysMenuMapper.findMenuDataPidTreeByRoleId(roleId,id);
        diguiTreee(childSysMenuList,roleId);
        return childSysMenuList;
    }
    public void diguiTreee(List<SysMenu> sysMenuList,Integer roleId){
        for(SysMenu sysMenu:sysMenuList){
            int pid=sysMenu.getId();
            List<SysMenu> childSysMenuList=sysMenuMapper.findMenuDataPidTreeByRoleId(roleId,pid);
            if (childSysMenuList!=null&& childSysMenuList.size()>0){
                sysMenu.setChecked(false);
                sysMenu.setChildren(childSysMenuList);
                diguiTreee(childSysMenuList,roleId);
            }
        }
    }

    //保存角色的权限--roleId和menuId
    @Override
    public int addCheckedMenuTreeData(String checkedMenuTreeDataJson, Integer roleId) {
        JSONArray jsonArray= JSONArray.parseArray(checkedMenuTreeDataJson);
        ArrayList<Integer> menuIdList=new ArrayList<>();
        for (int i = 0; i < jsonArray.size(); i++) {
            SysMenu sysMenu=jsonArray.getObject(i, SysMenu.class);
            menuIdList.add(sysMenu.getId());
            if(sysMenu.getChildren()!=null){
                digui(sysMenu.getChildren(),menuIdList);
            }
        }
        sysMenuMapper.reldeleteByRoleId(roleId);
        for (Integer integer : menuIdList) {
            sysMenuMapper.relinsert(new SysRoleMenuKey(Integer.toString(roleId),Integer.toString(integer)));
        }
        return menuIdList.size();
    }

    public void digui(List<SysMenu> sysMenuList,ArrayList<Integer> menuIdList){
        for (SysMenu sysMenu : sysMenuList) {
            menuIdList.add(sysMenu.getId());
            if(sysMenu.getChildren()!=null){
                digui(sysMenu.getChildren(),menuIdList);
            }
        }
    }

    @Override
    public List<SysMenu> getSysMenuTreeDataById(Integer id) {
        //获取当前节点的子节点
        List<SysMenu> childSysMenuList=sysMenuMapper.findMenuChildByPid(id);
        diguiTree(childSysMenuList);
        return childSysMenuList;
    }
    public void diguiTree(List<SysMenu> sysMenuList)
    {
        for (SysMenu sysMenu : sysMenuList) {
            int pid=sysMenu.getId();
            List<SysMenu> childSysMenuList=sysMenuMapper.findMenuChildByPid(pid);
            if(childSysMenuList!=null && childSysMenuList.size()>0) {
                sysMenu.setChildren(childSysMenuList);
                diguiTree(childSysMenuList);
            }
        }
    }


    /*
     * pojo就是一个SysMenu对象
     *   属性 id=1
     *   属性 roleId==5
     * */
    @Override
    public List<SysMenu> queryNavMenuTree(SysMenu sysMenu)
    {
        System.out.println("queryNavMenuTree roleId="+sysMenu.getRoleId());
        //获取根节点的子节点
        List<SysMenu> rootSysMenu=sysMenuMapper.queryMenuByPidAndRoleId(sysMenu);
        diguiNavMenuTree(rootSysMenu,sysMenu.getRoleId());
        return rootSysMenu;
    }


    /*
     * 实现递归查询
     * */
    public void  diguiNavMenuTree(List<SysMenu> list,Integer roleId)
    {
        for (SysMenu sysMenu : list) {
            sysMenu.setRoleId(roleId);
            List<SysMenu> subSysMenuList=sysMenuMapper.queryMenuByPidAndRoleId(sysMenu);
            //当前菜单有子菜单
            if(subSysMenuList!=null&& subSysMenuList.size()>0)
            {
                sysMenu.setChildren(subSysMenuList);
                diguiNavMenuTree(subSysMenuList,roleId);
            }
        }
    }

    @Override
    public List<SysMenu> query(SysMenu sysMenu) {
        return sysMenuMapper.query(sysMenu);
    }
}
